package com.lzw.DailyAudit.service.dealSourceData_01;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hooya.fa.eu.api.bo.stk.BuyRecordBO;
import com.hooya.fa.eu.api.bo.stk.HistoryBelongsBO;
import com.hooya.fa.eu.api.bo.stk.ReturnedRecordBO;
import com.hooya.fa.eu.api.bo.stk.SellRecordBO;
import com.hooya.fa.eu.api.enums.CkEnum;
import com.hooya.fa.eu.biz.DailyAudit.entity.DailyAuditStatus;
import com.hooya.fa.eu.biz.DailyAudit.mapper.DailyAuditStatusMapper;
import com.hooya.fa.eu.biz.DailyAudit.service.dateAggregation_02.CommonService;
import com.hooya.fa.eu.biz.dao.entity.*;
import com.hooya.fa.eu.biz.dao.entity.rxh.MetaStore;
import com.hooya.fa.eu.biz.dao.manager.AddEntityManager;
import com.hooya.fa.eu.biz.dao.manager.DictManager;
import com.hooya.fa.eu.biz.dao.manager.PkManager;
import com.hooya.fa.eu.biz.dao.manager.special.*;
import com.hooya.fa.eu.biz.dao.mapper.*;
import com.hooya.fa.eu.biz.dao.mapper.stk.TempPkpyMapper;
import com.hooya.fa.eu.biz.service.impl.StkCollectionServiceImpl;
import com.hooya.integrated.common.core.util.BeanCopyUtils;
import com.hooya.integrated.common.core.util.RedisUtils;
import com.hooya.integrated.common.util.times.DateExtUtil;
import com.xxl.job.core.context.XxlJobHelper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import static com.hooya.fa.eu.api.constant.DictConstant.DICT_PREFIX;

/**
 * @Description 1
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2024/1/29
 * @Version 1.0.0
 */
@Slf4j
@Service
@DS("fa_eu")
@AllArgsConstructor
public class DailyStkCollectionService {

	private final static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

	private final StkBuyMapper stkBuyMapper;

	private final TempPkpyMapper tempPkpyMapper;

	private final JcDataManager jcDataManager;

	private final SellDeliveryManager sellDeliveryManager;

	private final ReturnedGoodsManager returnedGoodsManager;

	private final JcHisBelongsManager jcHisBelongsManager;

	private final XsDataManager xsDataManager;

	private final FinalDataManager finalDataManager;

	private final DictManager dictManager;

	private final CkDictMapper ckDictMapper;

	private final AddEntityManager entityManager;

	private final PkManager pkManager;

	private final StkSellMapper stkSellMapper;

	private final StkCollectionServiceImpl stkCollectionService;

	private final DailyAuditStatusMapper statusMapper;

	private final TempKcjlDailyMapper tempKcjlDailyMapper;

	private final EuFbaMapper euFbaMapper;

	private final DictMapper dictMapper;

	private final OrderReturnedMapper orderReturnedMapper;

	private final ReturnedMapper returnedMapper;

	private final CommonService commonService;

	public void generatorCollectionData_daily(String startDate, int num) {

		try{
			String name = "generatorCollectionData-01";
			DailyAuditStatus dailyAuditStatus = statusMapper.selectStatus(name, startDate);
			if (dailyAuditStatus != null && dailyAuditStatus.getFlag() == 1) {
				log.info("{} - 该方法已执行，进行跳过！", name);
				return;
			}

			log.info("开始执行Stk审计数据插入任务");

			// 当前天开始 - 明天结束 日审计的时间设置
			String endDate = DateExtUtil.getAfterNDays(1, startDate);

			//仓库数据（库存）
			String yesterday = DateExtUtil.getYesterday(startDate);
			List<TempKcjlDaily> inventoryList = tempKcjlDailyMapper.getOpeningInventoryDaily(yesterday, null)
					.stream().filter(l -> !l.getPlace().equals("EU07")).collect(Collectors.toList());
			//------------------------------------ 各个字典 ------------------------------------
			//fba国家列表
			List<String> fbaCountryList = euFbaMapper.getFbaCountryListDaily();  //eu_fba_order_2303
			//虚拟仓库字典
			List<PlaceMatchDict> placeMatchDictList = dictMapper.getPlaceMatchListDaily(); // fa_eu.place_match_dict
			Map<String, PlaceMatchDict> placeMatchDictMap = placeMatchDictList.stream()
					.collect(Collectors.toMap(PlaceMatchDict::getVirtualWhId, i -> i));
			//店铺公司字典
			List<MetaStore> metaStoreList = dictMapper.getMetaStoreListDaily(); // fa_eu.meta_store
			Map<String, String> storeMap = metaStoreList.stream().collect(Collectors.toMap(l -> l.getStore().toLowerCase(), MetaStore::getCorp));
			//进仓公司字典
			Map<String, String> metaWarehouseMap;
			List<MetaWarehouse> metaWarehouseList = dictMapper.getMetaWarehouseListDaily(); // odw.meta_warehouse
			if (!CollectionUtils.isEmpty(metaWarehouseList)) {
				metaWarehouseMap = metaWarehouseList.stream()
						.collect(Collectors.toMap(MetaWarehouse::getWhId, MetaWarehouse::getCountry));
			} else {
				metaWarehouseMap = new HashMap<>();
			}
			//其他仓库字典
			List<CkDict> dictList = ckDictMapper.selectList(null); // fa_eu.ck_dict
			dictManager.cacheDictDaily(dictList);

			Map<String, String> fbaCkMap = RedisUtils.hmget(DICT_PREFIX + CkEnum.FBA_CK_MAP.getName());

			//平仓
			List<TempPkpy> closePositionPkpyList = pkManager.closePositionDaily(yesterday, startDate, inventoryList);  // fa_eu.pk
			inventoryList = inventoryList.stream().filter(l -> l.getSl() != 0).collect(Collectors.toList());

			//补单开始时间与补单日期 - 昨天 - 昨天的前一个月
			String suppDate = startDate;
			String suppStartDate = DateExtUtil.getBeforeNMonth(num == 0 ? 1 : num, startDate);

			//------------------------------------ 进仓 ------------------------------------
			List<BuyRecordBO> recordList = new LinkedList<>();
			//所有进仓记录（本月 + 历史）
			List<BuyRecordBO> warehousingList = stkBuyMapper.getWarehousingListByDateDaily(null, null); // jcjl_2303 jcjlsheet_2303 swfp_2303 odw.meta_warehouse + 排除了不属于我们货号（prd/cpbh）的数据
			//warehousingList进仓数据的处理
			jcDataManager.dealSourceJcData(df, warehousingList, recordList, placeMatchDictMap);
			//取出本月进仓数据
			List<BuyRecordBO> monthWarehousingList = jcDataManager.getCurMonthJcData(df, startDate, endDate, recordList);
			//------------------------------------ 转运 ------------------------------------
			//24年5月开始将EU07仓库全部转出，所以需要将转入和转出到该仓库的数据除去
			List<BuyRecordBO> zyList = getZyListByDateDaily(startDate, endDate);
			zyList.forEach(f -> f.setHthm(f.getHthm() != null ? f.getHthm() + "-" + startDate.split("-")[2] : ""));
			//转运补单数据(获取除盘亏盘盈差异意外的)
			List<BuyRecordBO> hisSupplementZyList = getSupplementZyListDaily(suppStartDate, startDate);  // jcjl_2303 jcjlsheet_2303 swfp_2303 odw.meta_warehouse zyjl zyjl_delete zyjl_change
			List<TempPkpy> hisPkpyList = tempPkpyMapper.getPkpyListByDateDaily(suppStartDate, startDate);
			List<BuyRecordBO> removeZyList = new LinkedList<>();
			if (!CollectionUtils.isEmpty(hisSupplementZyList)) {    // 相当于转运补单记录 如果存在 于 盘亏盘盈则删除 说明已经补过单了
				for (BuyRecordBO bo : hisSupplementZyList) {
					List<TempPkpy> pkpies = hisPkpyList.stream().filter(l -> l.getCpbh().equals(bo.getCpbh())
							&& l.getPlace().equals(bo.getZcck()) && Objects.equals(l.getSl(), bo.getCgsl())
							&& l.getDate().equals(bo.getHtrq())).collect(Collectors.toList());
					if (!CollectionUtils.isEmpty(pkpies)) {
						removeZyList.add(bo);
					}
				}
				hisSupplementZyList.removeAll(removeZyList);
				hisSupplementZyList.forEach(l -> l.setHtrq(suppDate));
				zyList.addAll(hisSupplementZyList);
				List<BuyRecordBO> suppSzkList = hisSupplementZyList.stream()
						.filter(l -> l.getZcck().equals("EU生资库")).collect(Collectors.toList());
				if (!CollectionUtils.isEmpty(suppSzkList)) {
					log.info("转运补单生资库，数据为：" + suppSzkList);
				}
				log.info("所有补充转运单的合同日期：" + suppDate + ",合同号码为：" +
						hisSupplementZyList.stream().map(BuyRecordBO::getHthm).distinct().collect(Collectors.toList()));
			}

			//------------------------------------ 销售发货前处理lywl为FBA_return的数据 ------------------------------------
			String name1 = "insertLywlIsFbaReturn-01";
			DailyAuditStatus dailyAuditStatus1 = statusMapper.selectStatus(name1, startDate);
			if (dailyAuditStatus1 != null && dailyAuditStatus1.getFlag() == 1) {
				log.info("{} - 该方法已执行，进行跳过！", name1);
			} else {
				List<SellRecordBO> sellRecordBOS = stkSellMapper.getlywlIsFbaReturnListDaily(startDate, endDate); //  xsjl_2303
				if (!sellRecordBOS.isEmpty()) {
					sellRecordBOS.forEach(sellRecordBO -> sellRecordBO.setCompany(storeMap.get(sellRecordBO.getDianPu())));
					List<TempXsjlAbnormal> tempXsjlAbnormalLywlIsFDS_ReturnList = sellRecordBOS.stream()
							.map(sellRecordBO ->
									BeanCopyUtils.copyPropertiesNew(sellRecordBO,
											TempXsjlAbnormal::new, (s, t) -> {
												t.setCreateTime(new Date());
											})
							)
							.collect(Collectors.toList());
					log.info("开始执行FbaReturnToXsjlAbnormal");
					stkSellMapper.insertLywlIsFbaReturnDaily(tempXsjlAbnormalLywlIsFDS_ReturnList);
					log.info("FbaReturnToXsjlAbnormal执行完成");
				}
				// 修改执行状态
				commonService.UpdateOrInsertStatus(dailyAuditStatus1, name1, startDate);
			}
			//------------------------------------ 销售发货 ------------------------------------

			List<SellRecordBO> sellDeliveryList = sellDeliveryManager
					.getSellDeliveryListByDateDaily(startDate, endDate, placeMatchDictMap, storeMap, fbaCountryList);
			//销售补单数据
			List<SellRecordBO> hisSellDeliveryList = sellDeliveryManager
					.getHisSellDeliveryListByDateDaily(suppStartDate, startDate, placeMatchDictMap, storeMap, fbaCountryList);
			if (!CollectionUtils.isEmpty(hisSellDeliveryList)) {
				log.info("销售补充数据有" + hisSellDeliveryList.size() + "条");
				hisSellDeliveryList.forEach(l -> l.setOrderDate(suppDate));
				sellDeliveryList.addAll(hisSellDeliveryList);
			}
			//校验移除销售发货已存在历史销售记录的数据
			sellDeliveryManager.checkSheetNumberDaily(sellDeliveryList, startDate);

			//------------------------------------ 退货 ------------------------------------
			List<String> returnedOrderIds = orderReturnedMapper.getReturnedOrderIdsByDateDaily(startDate, endDate); // order_eu
			List<ReturnedRecordBO> returnedList = getReturnedListByDateDaily(startDate, endDate, returnedOrderIds);  // order_eu sale_2303
			List<ReturnedRecordBO> resReturnedList =
					returnedGoodsManager.getEligibleReturnGoodsData(returnedList, placeMatchDictMap, storeMap);


			//修改历史进仓货权，并整合数据
			List<HistoryBelongsBO> allHisBelongsList =
					jcHisBelongsManager.editJcHisBelongsAndIntegrationDaily(recordList);
			//fba货权信息(做筛选优化用)
			List<HistoryBelongsBO> fbaBelongsList =
					jcHisBelongsManager.getFbaJcHisBelongsData(df, startDate, allHisBelongsList, fbaCkMap);

			//------------------------------------ 新增结果集 ------------------------------------
			//总的进仓记录
			List<TempJcjl> finalTempJcjlList = new LinkedList<>();
			//总的转运记录
			List<TempZyjl> finalTempZyjlList = new LinkedList<>();
			//总的仓库记录
			List<TempKcjlDaily> allInventoryList = new LinkedList<>();
			//总的买卖记录
			List<TradeRecord> finalTradeRecordList = new LinkedList<>();
			//盘亏盘盈集合
			List<TempPkpy> finalPkpyList = new LinkedList<>(closePositionPkpyList);
			List<TempThjl> finalTempThjlList = new LinkedList<>();
			List<TempXsjl> finalTempXsjlList = new LinkedList<>();
			List<TempXsjlAbnormal> finalTempXsjlAbnormalList = new LinkedList<>();
			List<TempXsjlDelete> finalTempXsjlDeleteList = new LinkedList<>();
			List<TempZyjlDelete> finalTempZyjlDeleteList = new ArrayList<>();
			List<TempZyjlChange> finalZyjlChangeList = new LinkedList<>();

			//==================================== 当月循环,整理数据 ====================================
			Date start = new Date(startDate.replace("-", "/"));
			Date end = new Date(endDate.replace("-", "/"));
			DateRange dateRange = DateUtil.range(start, end, DateField.DAY_OF_MONTH);
			Map<String, List<BuyRecordBO>> jcMap = monthWarehousingList.stream().collect(Collectors.groupingBy(BuyRecordBO::getHtrq));
			Map<String, List<BuyRecordBO>> zyMap = zyList.stream().collect(Collectors.groupingBy(BuyRecordBO::getHtrq));
			List<BuyRecordBO> fbaHisJcZy = BeanCopyUtils.copyListProperties(fbaBelongsList, BuyRecordBO::new);
			//fds公司删除订单计数
			AtomicInteger deleteFdsXsjl = new AtomicInteger(0);
			//其他
			Date createTime = new Date();
			long st = System.currentTimeMillis();
			for (DateTime time : dateRange) {
				String curDate = time.toDateStr();
				//边界控制
				if (curDate.equals(endDate)) {
					continue;
				}
				log.info(curDate);
				XxlJobHelper.log(curDate);
				//需要进行删除的转运数据(转运中，库存不存在，进仓不存在，转运中转入不存在（非fba逻辑）)
				List<TempZyjlDelete> tempZyjlDeleteList = new ArrayList<>();
				//总的买卖记录
				List<TradeRecord> tradeRecordList = new LinkedList<>();
				//总的进仓记录
				List<TempJcjl> tempJcjlList = new LinkedList<>();
				//总的转运记录
				List<TempZyjl> tempZyjlList = new LinkedList<>();
				//变更的转运记录(每轮循环结束，将相同条件合并，数量相加，以防万一)
				List<TempZyjlChange> zyjlChangeList = new LinkedList<>();
				List<BuyRecordBO> deleteZyRecord = new LinkedList<>();
				//盘亏盘盈集合
				List<TempPkpy> pkpyList = new LinkedList<>();

				//------------------------------------ 处理进仓转运数据 ------------------------------------
				stkCollectionService.dealWithJcZyData(curDate, fbaCkMap, jcMap, zyMap, inventoryList, tradeRecordList, tempJcjlList,
						tempZyjlList, zyjlChangeList, fbaHisJcZy, metaWarehouseMap, deleteZyRecord, pkpyList);
				if (!CollectionUtils.isEmpty(deleteZyRecord)) {
					tempZyjlDeleteList.addAll(BeanCopyUtils.copyListProperties(deleteZyRecord, TempZyjlDelete::new));
				}
				//进仓加入所有历史货权
				if (!CollectionUtils.isEmpty(tempJcjlList)) {
					List<HistoryBelongsBO> jcHisList = BeanCopyUtils
							.copyListProperties(tempJcjlList, HistoryBelongsBO::new, (s, t) -> t.setType("海运"));
					allHisBelongsList.addAll(jcHisList);
				}
				//转运加入所有历史货权
				if (!CollectionUtils.isEmpty(tempZyjlList)) {
					tempZyjlList = tempZyjlList.stream().filter(l -> l.getCgsl() != 0).collect(Collectors.toList());
					List<HistoryBelongsBO> zyHisList = BeanCopyUtils
							.copyListProperties(tempZyjlList, HistoryBelongsBO::new, (s, t) -> {
								t.setPlace(s.getZlck());
								t.setBelongs(s.getTobelongs());
								t.setType("转运");
							});
					allHisBelongsList.addAll(zyHisList);
				}
				List<HistoryBelongsBO> newAllHisBelongsList = allHisBelongsList.stream().distinct()
						.collect(Collectors.toList());
				//------------------------------------ 处理销售数据 ------------------------------------
				List<TempThjl> tempThjlList = new LinkedList<>();
				List<TempXsjl> tempXsjlList = new LinkedList<>();
				List<TempXsjlAbnormal> tempXsjlAbnormalList = new LinkedList<>();
				//当日需要处理的销售数据
				List<SellRecordBO> curSellData = sellDeliveryManager
						.dealCurSellDelivery(curDate, sellDeliveryList, newAllHisBelongsList);
				//销售区分fba、非fba数据
				List<HistoryBelongsBO> hisCkBelongsList = newAllHisBelongsList.stream()
						.filter(l -> {
							try {
								long dateTime = df.parse(l.getHtrq()).getTime();
								if (dateTime < df.parse(curDate).getTime()) {
									return true;
								}
							} catch (Exception e) {
								throw new RuntimeException(e.getMessage() + ",时间格式化失败！htrq：" + l.getHtrq());
							}
							return false;
						})
						.collect(Collectors.toList());
				//非fba所有历史货权信息
				List<HistoryBelongsBO> hisBelongList = hisCkBelongsList.stream()
						.filter(l -> StringUtils.isBlank(fbaCkMap.get(l.getPlace())))
						.collect(Collectors.toList());
				//fba
				List<HistoryBelongsBO> fbaHisBelongList = hisCkBelongsList.stream()
						.filter(l -> StringUtils.isNotBlank(fbaCkMap.get(l.getPlace())))
						.collect(Collectors.toList());
				xsDataManager.dealWithSellData(curDate, curSellData, hisBelongList, inventoryList,
						resReturnedList, metaWarehouseMap, tempThjlList, tempXsjlList);
				if (!CollectionUtils.isEmpty(tempThjlList)) {
					List<HistoryBelongsBO> thjl = BeanCopyUtils
							.copyListProperties(tempThjlList, HistoryBelongsBO::new, (s, t) -> {
								t.setHtrq(curDate);
								t.setType("退货");
							});
					newAllHisBelongsList.addAll(thjl);
				}
				xsDataManager.deductSellData(curSellData, hisBelongList, fbaHisBelongList, inventoryList,
						tempXsjlList, tradeRecordList, tempXsjlAbnormalList, metaWarehouseMap, deleteFdsXsjl);
				List<TempXsjlDelete> tempXsjlDeleteList = new LinkedList<>();
				List<TempKcjlDaily> turnPkpyRes = new ArrayList<>();
				List<TempKcjlDaily> allMinusInventoryList = inventoryList.stream().filter(l -> l.getSl() < 0)
						.collect(Collectors.toList());
				if (!CollectionUtils.isEmpty(allMinusInventoryList)) {
					turnPkpyRes.addAll(allMinusInventoryList);
					//盘亏盘盈入库
					pkpyList.addAll(BeanCopyUtils.copyListProperties(allMinusInventoryList, TempPkpy::new));
				}
				//进仓转运和
				finalDataManager.dealJcZyThenPkpy(tempJcjlList, tempZyjlList, turnPkpyRes, inventoryList, pkpyList);
				finalDataManager.dealFinalData(curDate, inventoryList, allInventoryList, tempXsjlList, tradeRecordList,
						tempXsjlAbnormalList, tempXsjlDeleteList);
				String dataDailyLog = "进仓：" + tempJcjlList.size() + ",仓库：" + inventoryList.size() + ",买卖：" + tradeRecordList.size() +
						",盘亏盘盈：" + pkpyList.size() + ",退货：" + tempThjlList.size() + ",销售：" + tempXsjlList.size() +
						",销售abnormal：" + tempXsjlAbnormalList.size() + ",销售delete：" + tempXsjlDeleteList.size() +
						",转运delete：" + tempZyjlDeleteList.size() + ",转运：" + tempZyjlList.size() +
						",转运change：" + zyjlChangeList.size() + ",删除销售单阈值：" + deleteFdsXsjl.get();
				log.info(dataDailyLog);
				XxlJobHelper.log(dataDailyLog);
				entityManager.dealDateRelation4Data(createTime, curDate, tempJcjlList, tempXsjlList, tempThjlList,
						tempZyjlList, tradeRecordList, pkpyList, tempXsjlAbnormalList, tempXsjlDeleteList,
						tempZyjlDeleteList, finalTempJcjlList, finalTempXsjlList, finalTempThjlList, finalTempZyjlList,
						finalTradeRecordList, finalPkpyList, finalTempXsjlAbnormalList, finalTempXsjlDeleteList,
						finalTempZyjlDeleteList, finalZyjlChangeList, zyjlChangeList, allInventoryList);
			}
			//新增数据
			String executeTimeLog = "转运进仓、销售（无盘亏盘盈） 执行总用时为：" + (System.currentTimeMillis() - st) / 1000 + "s";
			log.info(executeTimeLog);
			XxlJobHelper.log(executeTimeLog);
			//新增数据
			long addT = System.currentTimeMillis();
			entityManager.addStkDataDaily(finalTempJcjlList, finalTempZyjlList, allInventoryList, finalTradeRecordList,
					finalPkpyList, finalTempThjlList, finalTempXsjlList, finalTempXsjlAbnormalList,
					finalTempXsjlDeleteList, finalTempZyjlDeleteList, finalZyjlChangeList);
			// 修改执行状态
			commonService.UpdateOrInsertStatus(dailyAuditStatus, name, startDate);
			String addDataTimeLog = "新增数据用时：" + (System.currentTimeMillis() - addT) / 1000 + "s";
			String addDataSizeLog = "新增总数据量为：" + (finalTempJcjlList.size() + finalTempZyjlList.size() + allInventoryList.size() +
					finalTradeRecordList.size() + finalPkpyList.size() + finalTempThjlList.size() +
					finalTempXsjlList.size() + finalTempXsjlAbnormalList.size() + finalTempXsjlDeleteList.size() +
					finalTempZyjlDeleteList.size() + finalZyjlChangeList.size());
			log.info(addDataTimeLog + " - " + addDataSizeLog);
			XxlJobHelper.log(addDataTimeLog + " - " + addDataSizeLog);
		}catch (Exception e){
			log.error("generatorCollectionData方法执行失败", e);
			XxlJobHelper.log("generatorCollectionData方法执行失败");
		}

	}

	private List<ReturnedRecordBO> getReturnedListByDateDaily(String startDate, String endDate, List<String> returnedOrderIds) {
		List<ReturnedRecordBO> returnedEuOrderList = returnedMapper.getEuOrderList(startDate, endDate);
		if(CollectionUtil.isEmpty(returnedEuOrderList)){
			return new ArrayList<>();
		}
		List<ReturnedRecordBO> returnedSaleList = returnedMapper.getSaleList(returnedOrderIds);
		Map<String, ReturnedRecordBO> saleMap = new HashMap<>();
		if (returnedSaleList != null) returnedSaleList.forEach(f->saleMap.put(f.getOrderId(), f));

		List<ReturnedRecordBO> result = new ArrayList<>();
		for (ReturnedRecordBO euOrder : returnedEuOrderList) {
			if (euOrder.getOrderId() != null) {
				ReturnedRecordBO saleInfo = saleMap.get(euOrder.getOrderId());
				if (saleInfo != null) {
					euOrder.setDianPu(saleInfo.getDianPu());
					euOrder.setChck(saleInfo.getChck());
				}
			}
			result.add(euOrder);
		}

		// 去重处理
		return result.stream()
				.filter(Objects::nonNull)
				.collect(Collectors.collectingAndThen(
						Collectors.toCollection(LinkedHashSet::new),
						ArrayList::new));
	}

	private List<BuyRecordBO> getSupplementZyListDaily(String suppStartDate, String startDate) {
		// 获取三个表中的数据 (temp_zyjl, temp_zyjl_delete, temp_zyjl_change)
		List<TempZyjl> zyList = stkBuyMapper.getZyDataByDateDaily(suppStartDate, startDate);
		List<TempZyjl> zyDeleteList = stkBuyMapper.getZyDataDeleteByDateDaily(suppStartDate, startDate);
		List<TempZyjl> zyChangeList = stkBuyMapper.getZyDataChangeByDateDaily(suppStartDate, startDate);

		// 合并所有转运数据
		List<TempZyjl> allZyData = new ArrayList<>();
		allZyData.addAll(zyList);
		allZyData.addAll(zyDeleteList);
		allZyData.addAll(zyChangeList);

		// 将合并后的数据转换为sheetNumber集合，用于后续比较
		Set<String> sheetNumberSet = allZyData.stream()
				.map(TempZyjl::getSheetnumber)
				.filter(Objects::nonNull)
				.collect(Collectors.toSet());

		// 获取转运列表数据 (对应SQL中的zyListByDate部分)
		List<BuyRecordBO> zyListByDate = getZyListByDateDaily(suppStartDate, startDate);

		// 筛选出不在zy表中的记录，并满足其他条件
		return zyListByDate.stream()
				.filter(bo -> !sheetNumberSet.contains(bo.getSheetNumber())) // 对应 right join where zy.id is null  因为不存在，就不匹配，id就会为空
				.filter(bo -> bo.getCgsl() != null && bo.getCgsl() > 0) // 对应 x.cgsl != 0 and x.cgsl > 0
				.filter(bo -> !"HY-SCBYK25038730".equals(bo.getHthm())) // 对应 x.hthm != 'HY-SCBYK25038730'
				.collect(Collectors.toList());
	}

	private List<BuyRecordBO> getZyListByDateDaily(String startDate, String endDate) {
		List<BuyRecordBO> result = new ArrayList<>();
		List<BuyRecordBO> zyList = stkBuyMapper.getZyList(startDate, endDate);
		List<PlaceMatchDict> PlaceMatchDictList = stkBuyMapper.getPlaceMatchDict();
		// 创建字典映射，方便查找
		Map<String, PlaceMatchDict> dictMap = new HashMap<>();
		for (PlaceMatchDict dict : PlaceMatchDictList) {
			if ("ck".equals(dict.getType())) {
				dictMap.put(dict.getVirtualWhId().toUpperCase().trim(), dict);
			}
		}

		// 处理每条记录 - 进行转入转出仓库的切换
		for (BuyRecordBO record : zyList) {
			String zcck = record.getZcck();
			String zlck = record.getZlck();

			// 处理转出仓库
			if (zcck != null) {
				zcck = zcck.toUpperCase().trim();
				PlaceMatchDict zcckDict = dictMap.get(zcck);
				if (zcckDict != null) {
					record.setZcck(zcckDict.getRealWhId());
				} else {
					record.setZcck(zcck);
				}
			}

			// 处理转入仓库
			if (zlck != null) {
				zlck = zlck.toUpperCase().trim();
				PlaceMatchDict zlckDict = dictMap.get(zlck);
				if (zlckDict != null) {
					record.setZlck(zlckDict.getRealWhId());
				} else {
					record.setZlck(zlck);
				}
			}
		}

		// 过滤条件: zcck != zlck and zcck != 'EU07' and zlck != 'EU07'
		for (BuyRecordBO record : zyList) {
			String zcck = record.getZcck();
			String zlck = record.getZlck();

			if (zcck != null && zlck != null &&
					!zcck.equals(zlck) &&
					!"EU07".equals(zcck) &&
					!"EU07".equals(zlck)) {
				result.add(record);
			}
		}

		return result;
	}

}

